<template>
  <view
    class="dis-flex flex-column flex-x-center flex-y-center"
    style="width: 100%; height: 600rpx"
  >
    <u-loading mode="flower"></u-loading>
    <text style="font-weight: 700; margin-top: 20rpx">扫码处理中，请稍候</text>
    <view class="fixed-wrapper">
      <view class="bt-box">
        <view @click="goHome(false)" class="btn"> 首页 </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {}
  },
  onLoad(query) {
    let str = query.q || ''
    // https%3A%2F%2Fmdycjt.com%3Ftype%3Dpayment%26shopId%3D1915697490670272514%26memberNo%3D7901693201
    // https%3A%2F%2Fmdycjt.com%3Ftype%3Ddetail%26tradeOrderId%3D1961345126447382529
    console.debug('splash.onload.str', str)
    this.sceneDecode(str)
  },
  methods: {
    sceneDecode(str) {
      if (str === undefined || str === '') {
        this.$u.toast('扫码结果无效')
        this.goHome()
        return
      }
      const data = {}
      const decodeStr = decodeURIComponent(str)
      try {
        const params = decodeStr.split('?')[1].split('&')
        for (const i in params) {
          const kv = params[i].split('=')
          if (kv.length > 0 && kv[0]) {
            data[kv[0]] = kv[1]
          }
        }
        console.log('扫码结果解析成功：' + JSON.stringify(data))
      } catch (e) {
        console.warn('扫码结果解析出错：' + decodeStr + '\n' + e)
        this.$u.toast('扫码结果解析出错')
        this.goHome()
        return
      }
      // 所有二维码都可以携带邀请者的身份码
      if (data.memberNo) {
        uni.setStorageSync('saleMemberNo', data.memberNo)
      }
      // 邀请码，如：https://mdycjt.com?type=invite&memberNo=0248865801
      if (data.type === 'invite') {
        uni.reLaunch({
          url: '/pages/ucenter/ucenter'
        })
        return
      }
      // 招商码，如：https://mdycjt.com?type=agency&agencyId=1927990096452358145&memberNo=0248865801
      if (data.type === 'agency') {
        uni.setStorageSync('agencyId', data.agencyId)
        uni.reLaunch({
          url: '/pages/ucenter/ucenter'
        })
        return
      }
      // 收款码，如：https://mdycjt.com?type=payment&shopId=1915697490670272514&memberNo=7901693201
      if (data.type === 'payment') {
        uni.redirectTo({
          url: '/dine/pay-view/pay-view?shopId=' + data.shopId
        })
        return
      }
      // 外卖点餐码，如：https://mdycjt.com?type=takeout&shopId=1915697490670272514&memberNo=7901693201
      if (data.type === 'takeout') {
        uni.redirectTo({
          url: '/dine/diancan/diancanv2?shopId=' + data.shopId
        })
        return
      }
      // 堂食点餐码，如：https://mdycjt.com?type=dinein&shopId=1915697490670272514&deskNo=001&memberNo=7901693201
      if (data.type === 'dinein') {
        uni.redirectTo({
          url: '/dine/diancan/diancan?shopId=' + data.shopId + '&deskNo=' + data.deskNo
        })
        return
      }
      // 订单核销码，如：https://mdycjt.com?type=verify&verifyCode=06012224
      if (data.type === 'verify') {
        uni.redirectTo({
          url: '/store/scan?code=' + data.verifyCode
        })
        return
      }
      // 订单详情码，如：https://mdycjt.com?type=detail&tradeOrderId=1958609427688251394
      if (data.type === 'detail') {
        const userInfo = uni.getStorageSync('userInfo') || {}
        if (userInfo.isMerchant) {
          uni.redirectTo({
            url: '/store/order-details/order-details?id=' + data.tradeOrderId
          })
          return
        }
        if (userInfo.isRider) {
          uni.redirectTo({
            url: '/rider/order-details/order-details?id=' + data.tradeOrderId
          })
          return
        }
        uni.redirectTo({
          url: '/goods/order-details/order-details?id=' + data.tradeOrderId
        })
        return
      }
      // 其他
      this.$u.toast('二维码无效或不被支持')
      this.goHome()
    },
    goHome(timeout = true) {
      if (!timeout) {
        uni.reLaunch({
          url: '/pages/index/index'
        })
        return
      }
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/index/index'
        })
      }, 1500)
    }
  }
}
</script>

<style lang="scss" scoped>
.fixed-wrapper {
  position: fixed;
  width: 100%;
  bottom: 30rpx;
  left: 0;

  .bt-box {
    height: 96rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .btn {
    padding: 10rpx 80rpx;
    text-align: center;
    background: $uni-color-primary;
    border-radius: 40rpx;
    font-size: 28rpx;
    color: #ffffff;
  }
}
</style>
